arrow-navigation.helper.test.js ➔ expectActive   F
last analyzed

Complexity

Conditions 22

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
eloc 8
dl 0
loc 12
rs 0
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like arrow-navigation.helper.test.js ➔ expectActive often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import ArrowNavigationHelper from 'src/helper/arrow-navigation.helper';
2
import template from './arrow-navigation.helper.template.html';
3
4
const itemContainerSelector = 'ul.itemContainer';
5
const itemSelector = 'li';
6
7
describe('arrow-navigation.helper', () => {
8
    beforeEach(() => {
9
        document.body.innerHTML = template;
10
    });
11
12
    test('registers on element', () => {
13
        const input = document.querySelector('input');
14
        const navigationHelper = new ArrowNavigationHelper(
15
            input,
16
            itemContainerSelector,
17
            itemSelector,
18
            true
19
        );
20
21
        expect(navigationHelper._element).toBe(input);
22
        expect(navigationHelper._iterator).toBe(-1);
23
        expect(navigationHelper._parentSelector).toBe(itemContainerSelector);
24
        expect(navigationHelper._itemSelector).toBe(itemSelector);
25
    });
26
27
    test('move down through items infinite', () => {
28
        const input = document.querySelector('input');
29
        const navigationHelper = new ArrowNavigationHelper(
30
            input,
31
            itemContainerSelector,
32
            itemSelector
33
        );
34
35
        const keydownEvent = new Event('keydown');
0 ignored issues
show
Bug introduced by
The variable Event seems to be never declared. If this is a global, consider adding a /** global: Event */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
36
        keydownEvent.key = 'ArrowDown';
37
38
        for (let iteration = 0; iteration < 5; iteration++) {
39
            input.dispatchEvent(keydownEvent);
40
            expectActive(iteration % 3, navigationHelper);
41
        }
42
    });
43
44
    test('move down through items finite', () => {
45
        const input = document.querySelector('input');
46
        const navigationHelper = new ArrowNavigationHelper(
47
            input,
48
            itemContainerSelector,
49
            itemSelector,
50
            false
51
        );
52
53
        const keydownEvent = new Event('keydown');
0 ignored issues
show
Bug introduced by
The variable Event seems to be never declared. If this is a global, consider adding a /** global: Event */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
54
        keydownEvent.key = 'ArrowDown';
55
56
        for (let iteration = 0; iteration < 5; iteration++) {
57
            input.dispatchEvent(keydownEvent);
58
            expectActive(iteration < 3 ? iteration : 2, navigationHelper);
59
        }
60
    });
61
62
    test('move up through items infinite', () => {
63
        const input = document.querySelector('input');
64
        const navigationHelper = new ArrowNavigationHelper(
65
            input,
66
            itemContainerSelector,
67
            itemSelector,
68
            true
69
        );
70
71
        const keydownEvent = new Event('keydown');
0 ignored issues
show
Bug introduced by
The variable Event seems to be never declared. If this is a global, consider adding a /** global: Event */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
72
        keydownEvent.key = 'ArrowUp';
73
74
        for (let iteration = 0; iteration < 5; iteration++) {
75
            input.dispatchEvent(keydownEvent);
76
            expectActive(2 - (iteration % 3), navigationHelper);
77
        }
78
    });
79
80
    test('move up through items finite', () => {
81
        const input = document.querySelector('input');
82
        const navigationHelper = new ArrowNavigationHelper(
83
            input,
84
            itemContainerSelector,
85
            itemSelector,
86
            false
87
        );
88
89
        const keydownEvent = new Event('keydown');
0 ignored issues
show
Bug introduced by
The variable Event seems to be never declared. If this is a global, consider adding a /** global: Event */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
90
        keydownEvent.key = 'ArrowUp';
91
92
        for (let iteration = 0; iteration < 5; iteration++) {
93
            input.dispatchEvent(keydownEvent);
94
            expectActive(0, navigationHelper);
95
        }
96
    });
97
98
    test('it does nothing if noting is selected', () => {
99
        const linkCallback = jest.fn();
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
100
        const link = document.querySelector('a');
101
        link.addEventListener('click', linkCallback);
102
103
        const input = document.querySelector('input');
104
        const navigationHelper = new ArrowNavigationHelper(
0 ignored issues
show
Unused Code introduced by
The constant navigationHelper seems to be never used. Consider removing it.
Loading history...
105
            input,
106
            itemContainerSelector,
107
            itemSelector,
108
            true
109
        );
110
111
        const event = new Event('keydown');
0 ignored issues
show
Bug introduced by
The variable Event seems to be never declared. If this is a global, consider adding a /** global: Event */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
112
        event.key = 'Enter';
113
114
        input.dispatchEvent(event);
115
        expect(linkCallback).not.toHaveBeenCalled();
116
    });
117
118
    test('it does nothing if noting is selected', () => {
119
        const linkCallback = jest.fn();
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
120
        const link = document.querySelector('a');
121
        link.addEventListener('click', linkCallback);
122
123
        const input = document.querySelector('input');
124
        const navigationHelper = new ArrowNavigationHelper(
0 ignored issues
show
Unused Code introduced by
The constant navigationHelper seems to be never used. Consider removing it.
Loading history...
125
            input,
126
            itemContainerSelector,
127
            itemSelector,
128
            true
129
        );
130
131
        let event = new Event('keydown');
0 ignored issues
show
Bug introduced by
The variable Event seems to be never declared. If this is a global, consider adding a /** global: Event */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
132
        event.key = 'ArrowDown';
133
134
        input.dispatchEvent(event);
135
136
        event = new Event('keydown');
137
        event.key = 'Enter';
138
139
        input.dispatchEvent(event);
140
        expect(linkCallback).toHaveBeenCalled();
141
    });
142
143
    test('it does nothing on other keys than arrows and enter', () => {
144
        const linkCallback = jest.fn();
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
145
        const link = document.querySelector('a');
146
        link.addEventListener('click', linkCallback);
147
148
        const input = document.querySelector('input');
149
        const navigationHelper = new ArrowNavigationHelper(
150
            input,
151
            itemContainerSelector,
152
            itemSelector,
153
            true
154
        );
155
156
        const event = new Event('keydown');
0 ignored issues
show
Bug introduced by
The variable Event seems to be never declared. If this is a global, consider adding a /** global: Event */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
157
        event.key = 'tab';
158
159
        input.dispatchEvent(event);
160
161
        expect(navigationHelper._iterator).toBe(-1);
162
        expect(linkCallback).not.toBeCalled();
163
    });
164
165
    test('it does nothing if no items exists', () => {
166
        const items = document.querySelectorAll(itemSelector);
167
        items.forEach((node) => {
168
            node.remove();
169
        });
170
171
        const input = document.querySelector('input');
172
        const navigationHelper = new ArrowNavigationHelper(
173
            input,
174
            itemContainerSelector,
175
            itemSelector,
176
            true
177
        );
178
179
        const event = new Event('keydown');
0 ignored issues
show
Bug introduced by
The variable Event seems to be never declared. If this is a global, consider adding a /** global: Event */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
180
        event.key = 'tab';
181
182
        input.dispatchEvent(event);
183
184
        expect(navigationHelper._iterator).toBe(-1);
185
    });
186
187
    test('it does nothing if no container exists', () => {
188
        const items = document.querySelectorAll(itemContainerSelector);
189
        items.forEach((node) => {
190
            node.remove();
191
        });
192
193
        const input = document.querySelector('input');
194
        const navigationHelper = new ArrowNavigationHelper(
195
            input,
196
            itemContainerSelector,
197
            itemSelector,
198
            true
199
        );
200
201
        const event = new Event('keydown');
0 ignored issues
show
Bug introduced by
The variable Event seems to be never declared. If this is a global, consider adding a /** global: Event */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
202
        event.key = 'tab';
203
204
        input.dispatchEvent(event);
205
206
        expect(navigationHelper._iterator).toBe(-1);
207
    });
208
209
    function expectActive(index, navigationHelper) {
0 ignored issues
show
Bug introduced by
The function expectActive is declared conditionally. This is not supported by all runtimes. Consider moving it to root scope or using var expectActive = function() { /* ... */ }; instead.
Loading history...
210
        const items = document.querySelectorAll('li');
211
        expect(navigationHelper._iterator).toBe(index);
212
213
        for (let i = 0; i < items.length; i++) {
214
            if (i === index) {
215
                expect(items[i].classList).toContain('is-active');
216
            } else {
217
                expect(items[i].classList).not.toContain('is-active');
218
            }
219
        }
220
    }
221
});